package com.prs.crm.action.hr;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Salary;
import com.prs.crm.domain.hr.SalaryItem;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.hr.SalaryItemService;
import com.prs.crm.service.hr.SalaryService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class SalaryAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Salary salary;
	private SalaryService salaryService;
	private PaginationSupport pagination;
	private Integer[] salaryIds;

	private SalaryItemService salaryItemService;
	private Collection<SalaryItem> lstItem;

	private Integer[] items;
	private String[] num;

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;

	private Code code;
	private CodeService codeService;
	private String Encode;
	private Collection<CodeCommon> codelist;

	@Action("listSalary")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Salary.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSalaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectSalary")
	public String selectSalary() {
		this.execute();
		return SUCCESS;
	}

	@Action("editSalary")
	public String edit() {
		lstItem = this.getSalaryItemService().getAllSalaryItems();
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"薪酬");
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveSalary", results = {
			@Result(name = "success", type = "redirect", location = "editSalary"),
			@Result(name = "error", type = "chain", location = "editSalary") })
	public String save() {
		if (salary.getName() == null || "".equals(salary.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (salary.getSalarycode() == null
				|| "".equals(salary.getSalarycode().trim())) {
			addActionError("编码不能为空");
			return ERROR;
		}
		if (items == null) {
			addActionError("请把项目填写完整");
			return ERROR;
		}
		if ("n".equals(Encode)) {
			int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
					"薪酬", salary.getSalarycode());
			if (EncodeRplice != 0) {
				this.addActionError("编码重复!请重新输入");
				return ERROR;
			}
		}
		for (int i = 0; i < items.length; i++) {
			
			if (num[i] == null||"".equals(num[i]+"")) {
				addActionError("请把项目填写完整");
				return ERROR;
			}
			if(!ToolKits.isInteger(num[i])){
				addActionError("金额只能为数字类型");
				return ERROR;
			}

		}
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(salary.getSalarycode());
			codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			codes.setCodeMolde("薪酬");
			this.getCodeService().save(codes);
			salary.setCode(codes);
			this.getSalaryService().saveSalary(salary, items, num);
		} else {
			CodeCommon Vob = this.getCodeCommonService().getVobject(
					codeCommon.getId());
			Code codes = new Code();
			codes.setEntityName(this.getClass().getName());
			codes.setCodeValue(salary.getSalarycode());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.codeCommonService.saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			codes.setCodeMolde("薪酬");
			this.getCodeService().save(codes);
			salary.setCode(codes);
			this.getSalaryService().saveSalary(salary, items, num);
		}

		this.setPrompt("提交薪酬标准成功");
		salary.setState(Constrains.TASK_AUDITING);

		return SUCCESS;
	}

	@Action("viewSalary")
	public String view() {
		return SUCCESS;
	}

	@Action(value = "removeSalary", results = { @Result(name = "success", type = "redirect", location = "listSalary") })
	public String remove() {
		this.getSalaryService().batchRemove(salaryIds);
		return SUCCESS;
	}

	public Salary getSalary() {
		return salary;
	}

	public Integer[] getItems() {
		return items;
	}

	public void setItems(Integer[] items) {
		this.items = items;
	}

	public void setSalary(Salary salary) {
		this.salary = salary;
	}

	public SalaryService getSalaryService() {
		return salaryService;
	}

	public void setSalaryService(SalaryService salaryService) {
		this.salaryService = salaryService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getSalaryIds() {
		return salaryIds;
	}

	public void setSalaryIds(Integer[] salaryIds) {
		this.salaryIds = salaryIds;
	}

	public void setSalaryItemService(SalaryItemService salaryItemService) {
		this.salaryItemService = salaryItemService;
	}

	public SalaryItemService getSalaryItemService() {
		return salaryItemService;
	}

	public void setLstItem(Collection<SalaryItem> lstItem) {
		this.lstItem = lstItem;
	}

	public Collection<SalaryItem> getLstItem() {
		return lstItem;
	}
     
	
	
	public String[] getNum() {
		return num;
	}

	public void setNum(String[] num) {
		this.num = num;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

}
